home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / edebug / cl-read.el.z / cl-read.el
Encoding:
Text File  |  1998-05-21  |  49.4 KB  |  1,399 lines

  1. ;; Customizable, Common Lisp like reader for Emacs Lisp.
  2. ;; 
  3. ;; Copyright (C) 1993 by Guido Bosch <Guido.Bosch@loria.fr>
  4.  
  5. ;; This file is part of XEmacs
  6.  
  7. ;; XEmacs is free software; you can redistribute it and/or modify it
  8. ;; under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation; either version 2, or (at your option)
  10. ;; any later version.
  11.  
  12. ;; XEmacs is distributed in the hope that it will be useful, but
  13. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15. ;; General Public License for more details.
  16.  
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  19. ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  20. ;; 02111-1307, USA.
  21.  
  22. ;;; Synched up with: Not in FSF
  23.  
  24. ;;; Commentary:
  25.  
  26. ;; Please send bugs and comments to the author.
  27. ;;
  28. ;; <DISCLAIMER>
  29. ;; This program is still under development.  Neither the author nor
  30. ;; his employer accepts responsibility to anyone for the consequences of
  31. ;; using it or for whether it serves any particular purpose or works
  32. ;; at all.
  33.  
  34.  
  35. ;; Introduction
  36. ;; ------------
  37. ;;
  38. ;; This package replaces the standard Emacs Lisp reader (implemented
  39. ;; as a set of built-in Lisp function in C) by a flexible and
  40. ;; customizable Common Lisp like one (implemented entirely in Emacs
  41. ;; Lisp). During reading of Emacs Lisp source files, it is about 40%
  42. ;; slower than the built-in reader, but there is no difference in
  43. ;; loading byte compiled files - they dont contain any syntactic sugar
  44. ;; and are loaded with the built in subroutine `load'.
  45. ;;
  46. ;; The user level functions for defining read tables, character and
  47. ;; dispatch macros are implemented according to the Commom Lisp
  48. ;; specification by Steel's (2nd edition), but the read macro functions
  49. ;; themselves are implemented in a slightly different way, because the
  50. ;; basic character reading is done in an Emacs buffer, and not by
  51. ;; using the primitive functions `read-char' and `unread-char', as real
  52. ;; CL does.  To get 100% compatibility with CL, the above functions
  53. ;; (or their equivalents) must be implemented as subroutines.
  54. ;;
  55. ;; Another difference with real CL reading is that basic tokens (symbols
  56. ;; numbers, strings, and a few more) are still read by the original
  57. ;; built-in reader. This is necessary to get reasonable performance.
  58. ;; As a consquence, the read syntax of basic tokens can't be
  59. ;; customized.
  60.  
  61. ;; Most of the built-in reader syntax has been replaced by lisp
  62. ;; character macros: parentheses and brackets, simple and double
  63. ;; quotes, semicolon comments and the dot. In addition to that, the
  64. ;; following new syntax features are provided:
  65.  
  66. ;; Backquote-Comma-Atsign Macro: `(,el ,@list) 
  67. ;;
  68. ;; (the clumsy Emacs Lisp syntax (` ((, el) (,@ list))) is also
  69. ;; supported, but with one restriction: the blank behind the quote
  70. ;; characters is mandatory when using the old syntax. The cl reader
  71. ;; needs it as a landmark to distinguish between old and new syntax.
  72. ;; An example:
  73. ;;
  74. ;; With blanks, both readers read the same:
  75. ;; (` (, (head)) (,@ (tail))) -std-read->  (` (, (head)) (,@ (tail)))
  76. ;; (` (, (head)) (,@ (tail))) -cl-read->   (` (, (head)) (,@ (tail)))
  77. ;;
  78. ;; Without blanks, the form is interpreted differently by the two readers:
  79. ;; (`(,(head)) (,@(tail))) -std-read-> (` (, (head)) (,@ (tail)))
  80. ;; (`(,(head)) (,@(tail))) -cl-read->  ((` ((, ((head)))) ((,@ ((tail)))))
  81. ;;
  82. ;; 
  83. ;; Dispatch Character Macro" `#'
  84. ;;
  85. ;; #'<function>            function quoting
  86. ;; #\<character>        character syntax
  87. ;; #.<form>                read time evaluation
  88. ;; #p<path>, #P<path>         paths
  89. ;; #+<feature>, #-<feature>     conditional reading
  90. ;; #<n>=, #<n>#         tags for shared structure reading
  91. ;;
  92. ;; Other read macros can be added easily (see the definition of the
  93. ;; above ones in this file, using the functions `set-macro-character'
  94. ;; and `set-dispatch-macro-character')
  95. ;;
  96. ;; The Cl reader is mostly downward compatile, (exception: backquote
  97. ;; comma macro, see above). E.g., this file, which is written entirely
  98. ;; in the standard Emacs Lisp syntax, can be read and compiled with the
  99. ;; cl-reader activated (see Examples below). 
  100.  
  101. ;; This also works with package.el for Common Lisp packages.
  102.  
  103.  
  104. ;; Requirements
  105. ;; ------------
  106. ;; The package runs on Emacs 18 and Emacs 19 (FSF and Lucid) It is
  107. ;; built on top of Dave Gillespie's cl.el package (version 2.02 or
  108. ;; later).  The old one (from Ceazar Quiroz, still shiped with some
  109. ;; Emacs 19 disributions) will not do.
  110.  
  111. ;; Usage
  112. ;; -----
  113. ;; The package is implemented as a kind of minor mode to the
  114. ;; emacs-lisp-mode. As most of the Emacs Lisp files are still written
  115. ;; in the standard Emacs Lisp syntax, the cl reader is only activated
  116. ;; on elisp files whose property lines contain the following entry:
  117. ;;
  118. ;; -*- Read-Syntax: Common-Lisp -*-
  119. ;;
  120. ;; Note that both property name ("Read-Syntax") and value
  121. ;; ("Common-Lisp") are not case sensitive. There can also be other
  122. ;; properties in this line: 
  123. ;;
  124. ;; -*- Mode: Emacs-Lisp; Read-Syntax: Common-Lisp -*-
  125.  
  126. ;; Installation
  127. ;; ------------
  128. ;; Save this file in a directory where Emacs will find it, then
  129. ;; byte compile it (M-x byte-compile-file).
  130. ;;
  131. ;; A permanent installation of the package can be done in two ways:
  132. ;;
  133. ;; 1.) If you want to have the package always loaded, put this in your
  134. ;;     .emacs, or in just the files that require it:
  135. ;;
  136. ;; (require 'cl-read) 
  137. ;;
  138. ;; 2.) To load the cl-read package automatically when visiting an elisp
  139. ;;     file that needs it, it has to be installed using the
  140. ;;     emacs-lisp-mode-hook. In this case, put the following function
  141. ;;     definition and add-hook form in your .emacs:
  142. ;;
  143. ;; (defun cl-reader-autoinstall-function () 
  144. ;;   "Activates the Common Lisp style reader for emacs-lisp-mode buffers,
  145. ;; if the property line has a local variable setting like this: 
  146. ;; \;\; -*- Read-Syntax: Common-Lisp -*-"
  147. ;;
  148. ;;   (or (boundp 'local-variable-hack-done)
  149. ;;       (let (local-variable-hack-done
  150. ;;             (case-fold-search t))
  151. ;;         (hack-local-variables-prop-line 't)
  152. ;;         (cond 
  153. ;;          ((and (boundp 'read-syntax)
  154. ;;                read-syntax
  155. ;;                (string-match "^common-lisp$" (symbol-name read-syntax)))
  156. ;;           (require 'cl-read)
  157. ;;           (make-local-variable 'cl-read-active)
  158. ;;           (setq cl-read-active 't))))))
  159. ;;
  160. ;; (add-hook 'emacs-lisp-mode-hook 'cl-reader-autoinstall-function)
  161. ;;
  162. ;; The `cl-reader-autoinstall-function' function tests for the
  163. ;; presence of the correct Read-Syntax property in the first line of
  164. ;; the file and loads the cl-read package if necessary. cl-read
  165. ;; replaces the following standard elisp functions:
  166. ;;
  167. ;;     - read
  168. ;;     - read-from-string
  169. ;;     - eval-current-buffer
  170. ;;     - eval-buffer
  171. ;;     - eval-region
  172. ;;    - eval-expression (to call reader explicitly)
  173. ;;
  174. ;; There may be other built-in functions that need to be replaced
  175. ;; (e.g. load).  The behavior of the new reader function depends on
  176. ;; the value of the buffer local variable `cl-read-active': if it is
  177. ;; nil, they just call the original functions, otherwise they call the
  178. ;; cl reader. If the cl reader is active in a buffer, this is
  179. ;; indicated in the modeline by the string "CL" (minor mode like). 
  180. ;;
  181.  
  182. ;; Examples:
  183. ;; ---------
  184. ;; After having installed the package as described above, the
  185. ;; following forms can be evaluated (M-C-x) with the cl reader being
  186. ;; active. (make sure that the mode line displays "(Emacs-Lisp CL)")
  187. ;;
  188. ;; (setq whitespaces '(#\space #\newline #\tab))
  189. ;; (setq more-whitespaces `(#\page ,@whitespaces #\linefeed))
  190. ;; (setq whitespace-strings (mapcar #'char-to-string more-whitespaces))
  191. ;; 
  192. ;; (setq shared-struct '(#1=[hello world] #1# #1#))
  193. ;; (progn (setq cirlist '#1=(a b . #1#)) 't)
  194. ;;
  195. ;; This file, though written in standard Emacs Lisp syntax, can also be
  196. ;; compiled with the cl reader active: Type M-x byte-compile-file
  197.  
  198. ;; TO DO List: 
  199. ;; -----------
  200. ;; - Provide a replacement for load so that uncompiled cl syntax
  201. ;;   source file can be loaded, too.  For now prohibit loading un-bytecompiled.
  202. ;; - Do we really need the (require 'cl) dependency?   Yes.
  203. ;; - More read macros: #S for structs, #A for array, #X for hex, #nR for radix
  204. ;; - Refine the error signaling mechanism.
  205. ;;     - invalid-cl-read-syntax is now defined. what else?
  206.  
  207.  
  208. ; Change History
  209. ; $Log:    cl-read.el,v $
  210. ; Revision 1.19  94/03/21  19:59:24  liberte
  211. ; Add invalid-cl-read-syntax error symbol.
  212. ; Add reader::read-sexp and reader::read-sexp-func to allow customization
  213. ; based on the results of reading.
  214. ; Remove more dependencies on cl-package.
  215. ; Remove reader::eval-current-buffer, eval-buffer, and eval-region,
  216. ; and use elisp-eval-region package instead.
  217. ; Revision 1.18  94/03/04  23:42:24  liberte
  218. ; Fix typos in comments.
  219. ; Revision 1.17  93/11/24  12:04:09  bosch
  220. ; cl-packages dependency removed. `reader::read-constituent' and
  221. ; corresponding variables moved to cl-packages.el.
  222. ; Multi-line comment #| ... |# dispatch character read macro added.
  223. ; Revision 1.16  1993/11/23  10:21:02  bosch
  224. ; Patches from Daniel LaLiberte integrated.
  225. ;
  226. ; Revision 1.15  1993/11/18  21:21:10  bosch
  227. ; `reader::symbol-regexp1' modified.
  228. ;
  229. ; Revision 1.14  1993/11/17  19:06:32  bosch
  230. ; More characters added to `reader::symbol-characters'.
  231. ; `reader::read-constituent' modified.
  232. ; defpackage form added.
  233. ;
  234. ; Revision 1.13  1993/11/16  13:06:41  bosch
  235. ; - Symbol reading for CL package convention implemented.
  236. ;   Variables `reader::symbol-characters', `reader::symbol-regexp1' and
  237. ;   `reader::symbol-regexp2' and functions `reader::lookup-symbol' and
  238. ;   `reader::read-constituent' added.
  239. ; - Prefix for internal symbols is now "reader::" (Common Lisp
  240. ;   compatible).
  241. ; - Dispatch character macro #: for reading uninterned symbols added.
  242. ;
  243. ; Revision 1.12  1993/11/07  19:29:07  bosch
  244. ; Minor bug fix.
  245. ;
  246. ; Revision 1.11  1993/11/07  19:23:59  bosch
  247. ; Comment added. Character read macro #\<char> rewritten. Now reads 
  248. ; e.g. #\meta-control-x. Needs to be checked. 
  249. ; fix in `reader::restore-shared-structure'. `cl-reader-autoinstall-function' improved.
  250. ;
  251. ; Revision 1.10  1993/11/06  18:35:35  bosch
  252. ; Included Daniel LaLiberte's Patches.
  253. ; Efficiency of `reader::restore-shared-structure' improved.
  254. ; Implementation notes for shared structure reading added.
  255. ;
  256. ; Revision 1.9  1993/09/08  07:44:54  bosch
  257. ; Comment modified.
  258. ;
  259. ; Revision 1.8  1993/08/10  13:43:34  bosch
  260. ; Hook function `cl-reader-autoinstall-function' for automatic installation added.
  261. ; Buffer local variable `cl-read-active' added: together with the above
  262. ; hook it allows the file specific activation of the cl reader.
  263. ;
  264. ; Revision 1.7  1993/08/10  10:35:21  bosch
  265. ; Functions `read*' and `read-from-string*' renamed into `reader::read'
  266. ; and `reader::read-from-string'. Whitespace character skipping after
  267. ; recursive reader calls removed (Emacs 19 should not need this).
  268. ; Functions `cl-reader-install'  and `cl-reader-uninstall' updated.
  269. ; Introduction text and  function comments added.
  270. ;
  271. ; Revision 1.6 1993/08/09 15:36:05 bosch Function `read*' now nearly
  272. ; elisp compatible (no functions as streams, yet -- I don't think I
  273. ; will ever implement this, it would be far too slow).  Elisp
  274. ; compatible function `read-from-string*' added.  Replacements for
  275. ; `eval-current-buffer', `eval-buffer' and `eval-region' added.
  276. ; Renamed feature `cl-dg' in `cl', as Dave Gillespie's cl.el package
  277. ; is rather stable now.  Function `cl-reader-install' and
  278. ; `cl-reader-uninstall' modified.
  279. ;
  280. ; Revision 1.5  1993/08/09  10:23:35  bosch
  281. ; Functions `copy-readtable' and `set-syntax-from-character' added.
  282. ; Variable `reader::internal-standard-readtable' added.  Standard
  283. ; readtable initialization modified. Whitespace skipping placed back
  284. ; inside the read loop.
  285. ;
  286. ; Revision 1.4  1993/05/14  13:00:48  bosch
  287. ; Included patches from Daniel LaLiberte.
  288. ;
  289. ; Revision 1.3  1993/05/11  09:57:39  bosch
  290. ; `read*' renamed in `reader::read-from-buffer'. `read*' now can read
  291. ; from strings.
  292. ;
  293. ; Revision 1.2  1993/05/09  16:30:50  bosch
  294. ; (require 'cl-read) added.
  295. ; Calling of `{before,after}-read-hook' modified.
  296. ;
  297. ; Revision 1.1  1993/03/29  19:37:21  bosch
  298. ; Initial revision
  299. ;
  300. ;
  301.  
  302. ;;; Code:
  303.  
  304. (require 'cl)
  305. ;; Thou shalt evaluate a defadvice only once, or thou shalt surely lose. -sb
  306. (require 'advise-eval-region)
  307.  
  308. ;; load before compiling
  309. ;; This is ugly, but apparently the only way to do it :-(  -sb
  310. (provide 'cl-read)
  311. (require 'cl-read)
  312.  
  313. ;; bootstrapping with cl-packages
  314. ;; defpackage and in-package are ignored until cl-read is installed.
  315. '(defpackage reader
  316.   (:nicknames "rd")
  317.   (:use el)
  318.   (:export
  319.    cl-read-active
  320.    copy-readtable
  321.    set-macro-character
  322.    get-macro-character
  323.    set-syntax-from-character
  324.    make-dispatch-macro-character
  325.    set-dispatch-macro-character
  326.    get-dispatch-macro-character
  327.    before-read-hook
  328.    after-read-hook
  329.    cl-reader-install
  330.    cl-reader-uninstall
  331.    read-syntax
  332.    cl-reader-autoinstall-function))
  333.  
  334. '(in-package reader)
  335.  
  336.  
  337. (autoload 'compiled-function-p "bytecomp")
  338.  
  339. ;; This makes cl-read behave as a kind of minor mode: 
  340.  
  341. (make-variable-buffer-local 'cl-read-active)
  342. (defvar cl-read-active nil
  343.   "Buffer local variable that enables Common Lisp style syntax reading.")
  344. (setq-default cl-read-active nil)
  345.  
  346. (or (assq 'cl-read-active minor-mode-alist)
  347.     (setq minor-mode-alist
  348.       (cons '(cl-read-active " CL") minor-mode-alist)))
  349.  
  350. ;; Define a new error symbol: invalid-cl-read-syntax
  351. ;; XEmacs change
  352. (define-error 'invalid-cl-read-syntax "Invalid CL read syntax"
  353.   'invalid-read-syntax)
  354.  
  355. (defun reader::error (msg &rest args)
  356.   (signal 'invalid-cl-read-syntax (list (apply 'format msg args))))
  357.  
  358.  
  359. ;; The readtable
  360.  
  361. (defvar reader::readtable-size 256
  362.   "The size of a readtable."
  363.   ;; Actually, the readtable is a vector of size (1+
  364.   ;; reader::readtable-size), because the last element contains the
  365.   ;; symbol `readtable', used for defining `readtablep.
  366.   )
  367.  
  368. ;; An entry of the readtable must have one of the following forms:
  369. ;;
  370. ;; 1. A symbol, one of {illegal, constituent, whitespace}.  It means 
  371. ;;    the character's reader class.
  372. ;;
  373. ;; 2. A function (i.e., a symbol with a function definition, a byte
  374. ;;    compiled function or an uncompiled lambda expression).  It means the
  375. ;;    character is a macro character.
  376. ;;
  377. ;; 3. A vector of length `reader::readtable-size'. Elements of this vector
  378. ;;    may be `nil' or a function (see 2.). It means the character is a
  379. ;;    dispatch character, and the vector its dispatch function table.
  380.  
  381. (defvar *readtable*)
  382. (defvar reader::internal-standard-readtable)
  383.  
  384. (defun* copy-readtable 
  385.     (&optional (from-readtable *readtable*) 
  386.            (to-readtable 
  387.         (make-vector (1+ reader::readtable-size) 'illegal)))
  388.   "Return a copy of FROM-READTABLE \(default: *readtable*\). If the
  389. FROM-READTABLE argument is provided as `nil', make a copy of a
  390. standard \(CL-like\) readtable. If TO-READTABLE is provided, modify and
  391. return it, otherwise create a new readtable object."
  392.  
  393.   (if (null from-readtable)
  394.       (setq from-readtable reader::internal-standard-readtable))
  395.  
  396.   (loop for i to reader::readtable-size
  397.     as from-syntax = (aref from-readtable i)
  398.     do (setf (aref to-readtable i)
  399.          (if (vectorp from-syntax)
  400.              (copy-sequence from-syntax)
  401.            from-syntax))
  402.     finally return to-readtable))
  403.  
  404.  
  405. (defmacro reader::get-readtable-entry (char readtable)
  406.   (` (aref (, readtable) (, char))))
  407.    
  408. (defun set-macro-character 
  409.   (char function &optional readtable)
  410.     "Makes CHAR to be a macro character with FUNCTION as handler.
  411. When CHAR is seen by reader::read-from-buffer, it calls FUNCTION.
  412. Returns always t. Optional argument READTABLE is the readtable to set
  413. the macro character in (default: *readtable*)."
  414.   (or readtable (setq readtable *readtable*))
  415.   (or (reader::functionp function) 
  416.       (reader::error "Not valid character macro function: %s" function)) 
  417.   (setf (reader::get-readtable-entry char readtable) function)
  418.   t)
  419.  
  420.  
  421. (put 'set-macro-character 'edebug-form-spec 
  422.      '(&define sexp function-form &optional sexp))
  423. (put 'set-macro-character 'lisp-indent-function 1)
  424.  
  425. (defun get-macro-character (char &optional readtable)
  426.    "Return the function associated with the character CHAR.
  427. Optional READTABLE defaults to *readtable*. If char isn't a macro
  428. character in READTABLE, return nil."
  429.    (or readtable (setq readtable *readtable*))
  430.    (let ((entry (reader::get-readtable-entry char readtable)))
  431.      (if (reader::functionp entry) 
  432.      entry)))
  433.  
  434. (defun set-syntax-from-character 
  435.   (to-char from-char &optional to-readtable from-readtable)   
  436.   "Make the syntax of TO-CHAR be the same as the syntax of FROM-CHAR.
  437. Optional TO-READTABLE and FROM-READTABLE are the corresponding tables
  438. to use. TO-READTABLE defaults to the current readtable
  439. \(*readtable*\), and FROM-READTABLE to nil, meaning to use the
  440. syntaxes from the standard Lisp Readtable."
  441.   (or to-readtable (setq to-readtable *readtable*))
  442.   (or from-readtable 
  443.       (setq from-readtable reader::internal-standard-readtable))
  444.   (let ((from-syntax
  445.      (reader::get-readtable-entry from-char from-readtable)))
  446.     (if (vectorp from-syntax)
  447.     ;; dispatch macro character table
  448.     (setq from-syntax (copy-sequence from-syntax)))
  449.     (setf (reader::get-readtable-entry to-char to-readtable)
  450.       from-syntax))
  451.   t)
  452.  
  453.  
  454. ;; Dispatch macro character
  455. (defun make-dispatch-macro-character (char &optional readtable)
  456.   "Let CHAR be a dispatch macro character in READTABLE (default: *readtable*)."
  457.   (or readtable (setq readtable *readtable*))
  458.   (setf (reader::get-readtable-entry char readtable)
  459.     ;; create a dispatch character table 
  460.     (make-vector reader::readtable-size nil)))
  461.  
  462.  
  463. (defun set-dispatch-macro-character 
  464.   (disp-char sub-char function &optional readtable)
  465.   "Make reading CHAR1 followed by CHAR2 be handled by FUNCTION.
  466. Optional argument READTABLE (default: *readtable*).  CHAR1 must first be 
  467. made a dispatch char with `make-dispatch-macro-character'."
  468.   (or readtable (setq readtable *readtable*))
  469.   (let ((disp-table (reader::get-readtable-entry disp-char readtable)))
  470.     ;; check whether disp-char is a valid dispatch character
  471.     (or (vectorp disp-table)
  472.     (reader::error "`%c' not a dispatch macro character." disp-char))
  473.     ;; check whether function is a valid function 
  474.     (or (reader::functionp function) 
  475.     (reader::error "Not valid dispatch character macro function: %s" 
  476.                function))
  477.     (setf (aref disp-table sub-char) function)))
  478.  
  479. (put 'set-dispatch-macro-character 'edebug-form-spec
  480.      '(&define sexp sexp function-form &optional sexp))
  481. (put 'set-dispatch-macro-character 'lisp-indent-function 2)
  482.  
  483.  
  484. (defun get-dispatch-macro-character 
  485.   (disp-char sub-char &optional readtable)
  486.   "Return the macro character function for SUB-CHAR unser DISP-CHAR.
  487. Optional READTABLE defaults to *readtable*.
  488. Returns nil if there is no such function."
  489.   (or readtable (setq readtable *readtable*))
  490.   (let ((disp-table (reader::get-readtable-entry disp-char readtable)))
  491.     (and (vectorp disp-table)
  492.      (reader::functionp (aref disp-table sub-char))
  493.      (aref disp-table sub-char))))
  494.  
  495.  
  496. (defun reader::functionp (function)
  497.   ;; Check whether FUNCTION is a valid function object to be used 
  498.   ;; as (dispatch) macro character function.
  499.   (or (and (symbolp function) (fboundp function))
  500.       (compiled-function-p function)
  501.       (and (consp function) (eq (first function) 'lambda))))
  502.        
  503.  
  504. ;; The basic reader loop 
  505.  
  506. ;; shared and circular structure reading
  507. (defvar reader::shared-structure-references nil)
  508. (defvar reader::shared-structure-labels nil)
  509.  
  510. (defun reader::read-sexp-func (point func)
  511.   ;; This function is called to read a sexp at POINT by calling FUNC.
  512.   ;; reader::read-sexp-func is here to be advised, e.g. by Edebug,
  513.   ;; to do something before or after reading.
  514.   (funcall func))
  515.  
  516. (defmacro reader::read-sexp (point &rest body)
  517.   ;; Called to return a sexp starting at POINT.  BODY creates the sexp result
  518.   ;; and should leave point after the sexp.  The body is wrapped in
  519.   ;; a lambda expression and passed to reader::read-sexp-func.
  520.   (` (reader::read-sexp-func (, point) (function (lambda () (,@ body))))))
  521.  
  522. (put 'reader::read-sexp 'edebug-form-spec '(form body))
  523. (put 'reader::read-sexp 'lisp-indent-function 2)
  524. (put 'reader::read-sexp 'lisp-indent-hook 1)  ;; Emacs 18
  525.  
  526.  
  527. (defconst before-read-hook nil)
  528. (defconst after-read-hook nil)
  529.  
  530. ;; Set the hooks to `read-char' in order to step through the reader. e.g.
  531. ;; (add-hook 'before-read-hook '(lambda () (message "before") (read-char)))
  532. ;; (add-hook 'after-read-hook '(lambda () (message "after") (read-char)))
  533.  
  534. (defmacro reader::encapsulate-recursive-call (reader-call)
  535.   ;; Encapsulate READER-CALL, a form that contains a recursive call to
  536.   ;; the reader, for usage inside the main reader loop.  The macro
  537.   ;; wraps two hooks around READER-CALL: `before-read-hook' and
  538.   ;; `after-read-hook'.
  539.   ;;
  540.   ;; If READER-CALL returns normally, the macro exits immediately from
  541.   ;; the surrounding loop with the value of READER-CALL as result.  If
  542.   ;; it exits non-locally (with tag `reader-ignore'), it just returns
  543.   ;; the value of READER-CALL, in which case the surrounding reader
  544.   ;; loop continues its execution.
  545.   ;;
  546.   ;; In both cases, `before-read-hook' and `after-read-hook' are
  547.   ;; called before and after executing READER-CALL.
  548.   ;; Are there any other uses for these hooks?  Edebug doesn't need them.
  549.   (` (prog2
  550.      (run-hooks 'before-read-hook)
  551.      ;; this catch allows to ignore the return, in the case that
  552.      ;; reader::read-from-buffer should continue looping (e.g.
  553.      ;; skipping over comments)
  554.      (catch 'reader-ignore
  555.        ;; this only works inside a block (e.g., in a loop): 
  556.        ;; go outside 
  557.        (return 
  558.         (prog1 
  559.         (, reader-call)
  560.           ;; this occurrence of the after hook fires if the 
  561.           ;; reader-call returns normally ...
  562.           (run-hooks 'after-read-hook))))
  563.        ;; ... and that one if  it was thrown to the tag 'reader-ignore
  564.        (run-hooks 'after-read-hook))))
  565.  
  566. (put 'reader::encapsulate-recursive-call 'edebug-form-spec '(form))
  567. (put 'reader::encapsulate-recursive-call 'lisp-indent-function 0)
  568.  
  569. (defun reader::read-from-buffer (&optional stream reader::recursive-p)
  570.   (or (bufferp stream)
  571.       (reader::error "Sorry, can only read on buffers"))
  572.   (if (not reader::recursive-p)
  573.       ;; set up environment for shared structure reading
  574.       (let (reader::shared-structure-references
  575.         reader::shared-structure-labels
  576.         tmp-sexp)
  577.     ;; the reader returns an unshared sexpr, possibly containing
  578.     ;; symbolic references
  579.     (setq tmp-sexp (reader::read-from-buffer stream 't))
  580.     (if ;; sexpr actually contained shared structures
  581.         reader::shared-structure-references
  582.         (reader::restore-shared-structure tmp-sexp)
  583.       ;; it did not, so don't bother about restoring
  584.       tmp-sexp))
  585.  
  586.     (loop for char = (following-char)
  587.       for entry = (reader::get-readtable-entry  char *readtable*)
  588.       if (eobp) do (reader::error "End of file during reading")
  589.       do 
  590.       (cond 
  591.  
  592.        ((eq entry 'illegal)
  593.         (reader::error "`%c' has illegal character syntax" char))
  594.  
  595.        ;; skipping whitespace characters must be done inside this
  596.        ;; loop as character macro subroutines may return without
  597.        ;; leaving the loop using (throw 'reader-ignore ...)
  598.        ((eq entry 'whitespace)
  599.         (forward-char 1)  
  600.         ;; skip all whitespace
  601.         (while (eq 'whitespace 
  602.                (reader::get-readtable-entry  
  603.             (following-char) *readtable*))
  604.           (forward-char 1)))
  605.  
  606.        ;; for every token starting with a constituent character
  607.        ;; call the built-in reader (symbols, numbers, strings,
  608.        ;; characters with ?<char> syntax)
  609.        ((eq entry 'constituent)    
  610.         (reader::encapsulate-recursive-call
  611.          (reader::read-constituent stream)))
  612.  
  613.        ((vectorp entry)
  614.         ;; Dispatch macro character. The dispatch macro character
  615.         ;; function is contained in the vector `entry', at the
  616.         ;; place indicated by <sub-char>, the first non-digit
  617.         ;; character following the <disp-char>:
  618.         ;;     <disp-char><digit>*<sub-char>
  619.         (reader::encapsulate-recursive-call
  620.           (loop initially do (forward-char 1)
  621.             for sub-char = (prog1 (following-char) 
  622.                      (forward-char 1))
  623.             while (memq sub-char 
  624.                 '(?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9))
  625.             collect sub-char into digit-args
  626.             finally 
  627.             (return 
  628.              (funcall 
  629.               ;; no test is done here whether a non-nil
  630.               ;; contents is a correct dispatch character
  631.               ;; function to apply.
  632.               (or (aref entry sub-char)
  633.               (reader::error
  634.                "Undefined subsequent dispatch character `%c'" 
  635.                sub-char))
  636.               stream
  637.               sub-char 
  638.               (string-to-int
  639.                (apply 'concat 
  640.                   (mapcar 
  641.                    'char-to-string digit-args))))))))
  642.         
  643.        (t
  644.         ;; must be a macro character. In this case, `entry' is
  645.         ;; the function to be called
  646.         (reader::encapsulate-recursive-call
  647.           (progn 
  648.         (forward-char 1)
  649.         (funcall entry stream char))))))))
  650.  
  651.  
  652. ;; Constituent reader fix for Emacs 18
  653. (if (string-match "^19" emacs-version)
  654.     (defun reader::read-constituent (stream)
  655.       (reader::read-sexp (point)
  656.     (reader::original-read stream)))
  657.  
  658.   (defun reader::read-constituent (stream)
  659.     (reader::read-sexp (point)
  660.       (prog1 (reader::original-read stream)
  661.     ;; For Emacs 18, backing up is necessary because the `read' function 
  662.     ;; reads one character too far after reading a symbol or number.
  663.     ;; This doesnt apply to reading chars (e.g. ?n).
  664.     ;; This still loses for escaped chars.
  665.     (if (not (eq (reader::get-readtable-entry
  666.               (preceding-char) *readtable*) 'constituent))
  667.         (forward-char -1))))))
  668.  
  669.  
  670. ;; Make the default current CL readtable
  671.  
  672. (defconst *readtable*
  673.   (loop with raw-readtable = 
  674.     (make-vector (1+ reader::readtable-size) 'illegal)
  675.     initially do (setf (aref raw-readtable reader::readtable-size)
  676.                'readtable)
  677.     for entry in 
  678.     '((constituent ?! ?@ ?$ ?% ?& ?* ?_ ?- ?+ ?= ?/ ?\\ ?0 ?1 ?2
  679.                ?3 ?4 ?5 ?6 ?7 ?8 ?9 ?: ?~ ?> ?< ?a ?b
  680.                ?c ?d ?e ?f ?g ?h ?i ?j ?k ?l ?m ?n ?o ?p
  681.                ?q ?r ?s ?t ?u ?v ?w ?x ?y ?z ?A ?B ?C ?D
  682.                ?E ?F ?G ?H ?I ?J ?K ?L ?M ?N ?O ?P ?Q ?R
  683.                ?S ?T ?U ?V ?W ?X ?Y ?Z)
  684.       (whitespace ?  ?\t ?\n ?\r ?\f)
  685.  
  686.       ;; The following CL character classes are only useful for
  687.       ;; token parsing.  We don't need them, as token parsing is
  688.       ;; left to the built-in reader.
  689.       ;; (single-escape ?\\)
  690.       ;; (multiple-escape ?|)
  691.       )
  692.     do 
  693.     (loop for char in (rest entry)
  694.           do (setf (reader::get-readtable-entry  char raw-readtable)
  695.                (first entry)))
  696.     finally return raw-readtable)
  697.   "The current readtable.")
  698.  
  699.  
  700. ;; Variables used non-locally in the standard readmacros
  701. (defvar reader::context)
  702. (defvar reader::stack)
  703. (defvar reader::recursive-p)
  704.  
  705.  
  706. ;;;; Read macro character definitions
  707.  
  708. ;;; Hint for modifying, testing and debugging new read macros: All the
  709. ;;; read macros and dispatch character macros below are defined in
  710. ;;; the `*readtable*'.  Modifications or
  711. ;;; instrumenting with edebug are effective immediately without having to
  712. ;;; copy the internal readtable to the standard *readtable*.  However,
  713. ;;; if you wish to modify reader::internal-standard-readtable, then
  714. ;;; you must recopy *readtable*.
  715.  
  716. ;; Chars and strings
  717.  
  718. ;; This is defined to distinguish chars from constituents 
  719. ;; since chars are read by the standard reader without reading too far.
  720. (set-macro-character ?\?
  721.   (function
  722.    (lambda (stream char)
  723.      (forward-char -1)
  724.      (reader::read-sexp (point)
  725.        (reader::original-read stream)))))
  726.  
  727. ;; ?\M-\C-a
  728.  
  729. ;; This is defined to distinguish strings from constituents
  730. ;; since backing up after reading a string is simpler.
  731. (set-macro-character ?\"
  732.   (function
  733.    (lambda (stream char)
  734.      (forward-char -1)
  735.      (reader::read-sexp (point)
  736.        (prog1 (reader::original-read stream)
  737.      ;; This is not needed with Emacs 19, but it is OK.  See above.
  738.      (if (/= (preceding-char) ?\")
  739.          (forward-char -1)))))))
  740.  
  741. ;; Lists and dotted pairs
  742. (set-macro-character ?\( 
  743.   (function 
  744.    (lambda (stream char)
  745.      (reader::read-sexp (1- (point))
  746.        (catch 'read-list
  747.      (let ((reader::context 'list) reader::stack )
  748.        ;; read list elements up to a `.'
  749.        (catch 'dotted-pair
  750.          (while t
  751.            (setq reader::stack (cons (reader::read-from-buffer stream 't) 
  752.                      reader::stack))))
  753.        ;; In dotted pair. Read one more element
  754.        (setq reader::stack (cons (reader::read-from-buffer stream 't) 
  755.                      reader::stack)
  756.          ;; signal it to the closing paren
  757.          reader::context 'dotted-pair)
  758.        ;; Next char *must* be the closing paren that throws read-list
  759.        (reader::read-from-buffer stream 't)
  760.        ;; otherwise an error is signalled
  761.        (reader::error "Illegal dotted pair read syntax")))))))
  762.  
  763. (set-macro-character ?\) 
  764.   (function 
  765.    (lambda (stream char)
  766.      (cond ((eq reader::context 'list)
  767.         (throw 'read-list (nreverse reader::stack)))
  768.        ((eq reader::context 'dotted-pair)
  769.         (throw 'read-list (nconc (nreverse (cdr reader::stack)) 
  770.                      (car reader::stack))))
  771.        (t 
  772.         (reader::error "`)' doesn't end a list"))))))
  773.     
  774. (set-macro-character ?\.
  775.   (function 
  776.    (lambda (stream char)
  777.      (and (eq reader::context 'dotted-pair) 
  778.       (reader::error "No more than one `.' allowed in list"))
  779.      (throw 'dotted-pair nil))))
  780.  
  781. ;; '(#\a . #\b)
  782. ;; '(a . (b . c))
  783.  
  784. ;; Vectors: [a b]
  785. (set-macro-character ?\[
  786.   (function
  787.    (lambda (stream char)
  788.      (reader::read-sexp (1- (point))
  789.        (let ((reader::context 'vector))
  790.      (catch 'read-vector
  791.        (let ((reader::context 'vector)
  792.          reader::stack)
  793.          (while t (push (reader::read-from-buffer stream 't)
  794.                 reader::stack)))))))))
  795.  
  796. (set-macro-character ?\] 
  797.   (function 
  798.    (lambda (stream char)
  799.      (if (eq reader::context 'vector)
  800.      (throw 'read-vector (apply 'vector (nreverse reader::stack)))
  801.        (reader::error "`]' doesn't end a vector"))))) 
  802.  
  803. ;; Quote and backquote/comma macro
  804. (set-macro-character ?\'
  805.   (function
  806.    (lambda (stream char)
  807.      (reader::read-sexp (1- (point))
  808.        (list (reader::read-sexp (point) 'quote)
  809.          (reader::read-from-buffer stream 't))))))
  810.  
  811. (set-macro-character ?\`
  812.   (function
  813.    (lambda (stream char)
  814.      (if (= (following-char) ?\ )
  815.      ;; old backquote syntax. This is ambigous, because 
  816.      ;; (`(sexp)) is a valid form in both syntaxes, but 
  817.      ;; unfortunately not the same. 
  818.      ;; old syntax: read -> (` (sexp))
  819.      ;; new syntax: read -> ((` (sexp)))
  820.      (reader::read-sexp (1- (point)) '\`)
  821.        (reader::read-sexp (1- (point))
  822.      (list (reader::read-sexp (point) '\`)
  823.            (reader::read-from-buffer stream 't)))))))
  824.  
  825. (set-macro-character ?\,
  826.   (function
  827.    (lambda (stream char)
  828.      (cond ((eq (following-char) ?\ )
  829.         ;; old syntax
  830.         (reader::read-sexp (point) '\,))
  831.        ((eq (following-char) ?\@)
  832.         (forward-char 1)
  833.         (cond ((eq (following-char) ?\ )
  834.            (reader::read-sexp (point) '\,\@))
  835.           (t
  836.            (reader::read-sexp (- (point) 2)
  837.              (list 
  838.               (reader::read-sexp (point) '\,\@)
  839.               (reader::read-from-buffer stream 't))))))
  840.        (t
  841.         (reader::read-sexp (1- (point))
  842.           (list
  843.            (reader::read-sexp (1- (point)) '\,)
  844.            (reader::read-from-buffer stream 't))))))))
  845.  
  846. ;; 'a
  847. ;; '(a b c)
  848. ;; (let ((a 10) (b '(20 30))) `(,a ,@b c))
  849. ;; the old syntax is also supported:
  850. ;; (let ((a 10) (b '(20 30))) (` ((, a) (,@ b) c)))
  851.  
  852. ;; Single line character comment:  ; 
  853. (set-macro-character ?\;
  854.   (function
  855.    (lambda (stream char)
  856.      (skip-chars-forward "^\n\r")
  857.      (throw 'reader-ignore nil))))
  858.  
  859.  
  860.  
  861. ;; Dispatch character character #
  862. (make-dispatch-macro-character ?\#)
  863.  
  864. (defsubst reader::check-0-infix (n)
  865.   (or (= n 0) 
  866.       (reader::error "Numeric infix argument not allowed: %d" n)))
  867.  
  868.  
  869. (defalias 'search-forward-regexp 're-search-forward)
  870.  
  871. ;; nested multi-line comments #| ... |#
  872. (set-dispatch-macro-character ?\# ?\|
  873.   (function 
  874.    (lambda (stream char n)
  875.      (reader::check-0-infix n)
  876.      (let ((counter 0))
  877.        (while (search-forward-regexp "#|\\||#" nil t)
  878.      (if (string-equal
  879.           (buffer-substring
  880.            (match-beginning 0) (match-end 0))
  881.           "|#")
  882.          (cond ((> counter 0)
  883.             (decf counter))
  884.            ((= counter 0)
  885.             ;; stop here
  886.             (goto-char (match-end 0))
  887.             (throw 'reader-ignore nil))
  888.            ('t
  889.             (reader::error "Unmatching closing multicomment")))
  890.        (incf counter)))
  891.        (reader::error "Unmatching opening multicomment")))))
  892.  
  893. ;; From cl-packages.el
  894. (defconst reader::symbol-characters "[A-Za-z0-9-_!@$%^&*+=|~{}<>/]")
  895. (defconst reader::symbol-regexp2
  896.   (format "\\(%s+\\)" reader::symbol-characters))
  897.  
  898. (set-dispatch-macro-character ?\# ?\:
  899.   (function
  900.    (lambda (stream char n)
  901.      (reader::check-0-infix n)
  902.      (or (looking-at reader::symbol-regexp2)
  903.      (reader::error "Invalid symbol read syntax"))
  904.      (goto-char (match-end 0))
  905.      (make-symbol 
  906.       (buffer-substring (match-beginning 0) (match-end 0))))))
  907.  
  908. ;; Function quoting: #'<function>
  909. (set-dispatch-macro-character ?\# ?\'
  910.   (function
  911.    (lambda (stream char n)
  912.      (reader::check-0-infix n)
  913.      ;; Probably should test if cl is required by current buffer.
  914.      ;; Currently, cl will always be a feature because cl-read requires it.
  915.      (reader::read-sexp (- (point) 2)
  916.        (list 
  917.     (reader::read-sexp (point) (if (featurep 'cl)  'function* 'function))
  918.     (reader::read-from-buffer stream 't))))))
  919.  
  920. ;; Character syntax: #\<char> 
  921. ;; Not yet implemented: #\Control-a #\M-C-a etc. 
  922. ;; This definition is not used - the next one is more general.
  923. '(set-dispatch-macro-character ?# ?\\
  924.   (function 
  925.    (lambda (stream char n)
  926.      (reader::check-0-infix n)
  927.      (let ((next (following-char))
  928.            name)
  929.        (if (not (and (<= ?a next) (<= next ?z)))
  930.            (progn (forward-char 1) next)
  931.          (setq next (reader::read-from-buffer stream t))
  932.          (cond ((symbolp next) (setq name (symbol-name next)))
  933.                ((integerp next) (setq name (int-to-string next))))
  934.          (if (= 1 (length name))
  935.              (string-to-char name)
  936.            (case next
  937.              (linefeed  ?\n)
  938.              (newline   ?\r)
  939.              (space     ?\ )
  940.              (rubout    ?\b)
  941.              (page      ?\f)
  942.              (tab       ?\t)
  943.              (return    ?\C-m)
  944.              (t
  945.               (reader::error "Unknown character specification `%s'"
  946.                  next))))))))
  947.   )
  948.  
  949. (defvar reader::special-character-name-table
  950.   '(("linefeed"    . ?\n)
  951.     ("newline"    . ?\r)
  952.     ("space"    . ?\ )
  953.     ("rubout"    . ?\b)
  954.     ("page"    . ?\f)
  955.     ("tab"        . ?\t)
  956.     ("return"    . ?\C-m)))
  957.  
  958. (set-dispatch-macro-character ?# ?\\
  959.   (function 
  960.    (lambda (stream char n)
  961.      (reader::check-0-infix n)
  962.      (forward-char -1)
  963.      ;; We should read in a special package to avoid creating symbols.
  964.      (let ((symbol (reader::read-from-buffer stream t))
  965.        (case-fold-search t)
  966.        name modifier character char-base)
  967.        (setq name (symbol-name symbol))
  968.        (if (string-match "^\\(meta-\\|m-\\|control-\\|c-\\)+" name)
  969.        (setq modifier (substring name
  970.                      (match-beginning 1)
  971.                      (match-end 1))
  972.          character (substring name (match-end 1)))
  973.      (setq character name))
  974.        (setq char-base 
  975.          (cond ((= (length character) 1)
  976.             (string-to-char character))
  977.            ('t 
  978.             (cdr (assoc character 
  979.                 reader::special-character-name-table)))))
  980.        (or char-base 
  981.        (reader::error
  982.         "Unknown character specification `%s'" character))
  983.     
  984.        (and modifier
  985.         (progn 
  986.           (and (string-match "control-\\|c-" modifier)
  987.            (decf char-base 32))
  988.           (and (string-match "meta-\\|m-" modifier)
  989.            (incf char-base 128))))
  990.        char-base))))
  991.  
  992. ;; '(#\meta-space #\tab #\# #\> #\< #\a #\A  #\return #\space)
  993. ;; (eq #\m-tab ?\M-\t)
  994. ;; (eq #\c-m-x #\m-c-x)
  995. ;; (eq #\Meta-Control-return #\M-C-return)
  996. ;; (eq #\m-m-c-c-x #\m-c-x)
  997. ;; #\C-space #\C-@ ?\C-@
  998.  
  999.  
  1000.  
  1001. ;; Read and load time evaluation:  #.<form>
  1002. ;; Not yet implemented: #,<form>
  1003. (set-dispatch-macro-character ?\# ?\.
  1004.   (function 
  1005.    (lambda (reader::stream reader::char reader::n)
  1006.      (reader::check-0-infix reader::n)
  1007.      ;; This eval will see all internal vars of reader, 
  1008.      ;; e.g. stream, reader::recursive-p.  Anything that might be bound.
  1009.      ;; We must use `read' here rather than read-from-buffer with 'recursive-p
  1010.      ;; because the expression must not have unresolved #n#s in it anyway.
  1011.      ;; Otherwise the top-level expression must be completely read before
  1012.      ;; any embedded evaluation(s) occur(s).  CLtL2 does not specify this.
  1013.      ;; Also, call `read' so that it may be customized, by e.g. Edebug
  1014.      (eval (read reader::stream)))))
  1015. ;; '(#.(current-buffer) #.(get-buffer "*scratch*"))
  1016.  
  1017. ;; Path names (kind of):  #p<string>, #P<string>,
  1018. (set-dispatch-macro-character ?\# ?\P
  1019.   (function 
  1020.    (lambda (stream char n)
  1021.      (reader::check-0-infix n)
  1022.      (let ((string (reader::read-from-buffer stream 't)))
  1023.        (or (stringp string) 
  1024.        (reader::error "Pathname must be a string: %s" string))
  1025.        (expand-file-name string)))))
  1026.  
  1027. (set-dispatch-macro-character ?\# ?\p
  1028.   (get-dispatch-macro-character ?\# ?\P))
  1029.  
  1030. ;; #P"~/.emacs"
  1031. ;; #p"~root/home" 
  1032.  
  1033. ;; Feature reading:  #+<feature>,  #-<feature>
  1034. ;; Not yet implemented: #+<boolean expression>, #-<boolean expression>
  1035.  
  1036.  
  1037. (defsubst reader::read-feature (stream char n flag)
  1038.   (reader::check-0-infix n)
  1039.   (let (;; Use the original reader to only read the feature.
  1040.     ;; This is not exactly correct without *read-suppress*.
  1041.     ;; Also Emacs 18 read goes one too far,
  1042.     ;; so we assume there is a space after the feature.
  1043.     (feature (reader::original-read stream))
  1044.     (object (reader::read-from-buffer stream 't)))
  1045.     (if (eq (featurep feature) flag)
  1046.     object
  1047.       ;; Ignore it.
  1048.       (throw 'reader-ignore nil))))
  1049.  
  1050. (set-dispatch-macro-character ?\# ?\+
  1051.   (function 
  1052.    (lambda (stream char n)
  1053.      (reader::read-feature stream char n t))))
  1054.  
  1055. (set-dispatch-macro-character ?\# ?\-
  1056.   (function 
  1057.    (lambda (stream char n)
  1058.      (reader::read-feature stream char n nil))))
  1059.  
  1060. ;; (#+cl loop #+cl do #-cl while #-cl t (body))
  1061.  
  1062.  
  1063.  
  1064.  
  1065. ;; Shared structure reading: #<n>=, #<n>#
  1066.  
  1067. ;; Reading of sexpression with shared and circular structure read
  1068. ;; syntax  is done in two steps:
  1069. ;; 
  1070. ;; 1. Create an sexpr with unshared structures, just as the ordinary
  1071. ;;    read macros do, with two exceptions: 
  1072. ;;    - each label (#<n>=) creates, as a side effect, a symbolic
  1073. ;;      reference for the sexpr that follows it
  1074. ;;    - each reference (#<n>#) is replaced by the corresponding
  1075. ;;      symbolic reference. 
  1076. ;;
  1077. ;; 2. This non-cyclic and unshared lisp structure is given to the
  1078. ;;    function `reader::restore-shared-structure' (see
  1079. ;;    `reader::read-from-buffer'), which simply replaces
  1080. ;;    destructively all symbolic references by the lisp structures the
  1081. ;;    references point at. 
  1082. ;;
  1083. ;; A symbolic reference is an uninterned symbol whose name is obtained
  1084. ;; from the label/reference number using the function `int-to-string': 
  1085. ;;
  1086. ;; There are two non-locally used variables (bound in
  1087. ;; `reader::read-from-buffer') which control shared structure reading: 
  1088. ;; `reader::shared-structure-labels': 
  1089. ;;    A list of integers that correspond to the label numbers <n> in
  1090. ;;      the string currently read. This is used to avoid multiple
  1091. ;;      definitions of the same label.
  1092. ;; `reader::shared-structure-references': 
  1093. ;;      The list of symbolic references that will be used as temporary
  1094. ;;      placeholders for the shared objects introduced by a reference
  1095. ;;      with the same number identification.
  1096.  
  1097. (set-dispatch-macro-character ?\# ?\=
  1098.   (function 
  1099.    (lambda (stream char n)
  1100.      (and (= n 0) (reader::error "0 not allowed as label"))
  1101.      ;; check for multiple definition of the same label
  1102.      (if (memq n reader::shared-structure-labels)
  1103.      (reader::error "Label defined twice")
  1104.        (push n reader::shared-structure-labels))
  1105.      ;; create an uninterned symbol as symbolic reference for the label
  1106.      (let* ((string (int-to-string n))
  1107.         (ref (or (find string reader::shared-structure-references
  1108.                :test 'string=)
  1109.              (first 
  1110.               (push (make-symbol string) 
  1111.                 reader::shared-structure-references)))))
  1112.        ;; the link between the symbolic reference and the lisp
  1113.        ;; structure it points at is done using the symbol value cell
  1114.        ;; of the reference symbol.
  1115.        (setf (symbol-value ref) 
  1116.          ;; this is also the return value 
  1117.          (reader::read-from-buffer stream 't))))))
  1118.  
  1119.  
  1120. (set-dispatch-macro-character ?\# ?\#
  1121.   (function
  1122.    (lambda (stream char n)
  1123.      (and (= n 0) (reader::error "0 not allowed as label"))
  1124.      ;; use the non-local variable `reader::recursive-p' (from the reader
  1125.      ;; main loop) to detect labels at the top level of an sexpr.
  1126.      (if (not reader::recursive-p)
  1127.      (reader::error "References at top level not allowed"))
  1128.      (let* ((string (int-to-string n))
  1129.         (ref (or (find string reader::shared-structure-references
  1130.                :test 'string=)
  1131.              (first
  1132.               (push (make-symbol string) 
  1133.                 reader::shared-structure-references)))))
  1134.        ;; the value of reading a #n# form is a reference symbol
  1135.        ;; whose symbol value is or will be the shared structure. 
  1136.        ;; `reader::restore-shared-structure' then replaces the symbol by
  1137.        ;; its value.
  1138.        ref))))
  1139.  
  1140. (defun reader::restore-shared-structure (obj)
  1141.   ;; traverses recursively OBJ and replaces all symbolic references by
  1142.   ;; the objects they point at. Remember that a symbolic reference is
  1143.   ;; an uninterned symbol whose value is the object it points at. 
  1144.   (cond 
  1145.    ((consp obj)
  1146.     (loop for rest on obj
  1147.       as lastcdr = rest
  1148.       do
  1149.       (if;; substructure is a symbolic reference
  1150.           (memq (car rest) reader::shared-structure-references)
  1151.           ;; replace it by its symbol value, i.e. the associated object
  1152.           (setf (car rest) (symbol-value (car rest)))
  1153.         (reader::restore-shared-structure (car rest)))
  1154.       finally 
  1155.       (if (memq (cdr lastcdr) reader::shared-structure-references)
  1156.           (setf (cdr lastcdr) (symbol-value (cdr lastcdr)))
  1157.         (reader::restore-shared-structure (cdr lastcdr)))))
  1158.    ((vectorp obj)
  1159.     (loop for i below (length obj)
  1160.       do
  1161.       (if;; substructure  is a symbolic reference
  1162.           (memq (aref obj i) reader::shared-structure-references)
  1163.           ;; replace it by its symbol value, i.e. the associated object
  1164.           (setf (aref obj i) (symbol-value (aref obj i)))
  1165.         (reader::restore-shared-structure (aref obj i))))))
  1166.   obj)
  1167.  
  1168.  
  1169. ;; #1=(a b #3=[#2=c])
  1170. ;; (#1=[#\return #\a] #1# #1#)
  1171. ;; (#1=[a b c] #1# #1#)
  1172. ;; #1=(a b . #1#)
  1173.  
  1174. ;; Creation and initialization of an internal standard readtable. 
  1175. ;; Do this after all the macros and dispatch chars above have been defined.
  1176.  
  1177. (defconst reader::internal-standard-readtable (copy-readtable)
  1178.   "The original (CL-like) standard readtable. If you ever modify this
  1179. readtable, you won't be able to recover a standard readtable using
  1180. \(copy-readtable nil\)")
  1181.  
  1182.  
  1183. ;; Replace built-in functions that call the built-in reader
  1184. ;; 
  1185. ;; The following functions are replaced here: 
  1186. ;;
  1187. ;; read            by    reader::read
  1188. ;; read-from-string    by    reader::read-from-string
  1189. ;;
  1190. ;; eval-expression    by    reader::eval-expression
  1191. ;; Why replace eval-expression? Not needed for Lucid Emacs since the
  1192. ;; reader for arguments is also written in Lisp, and so may be overridden.
  1193. ;;
  1194. ;; eval-current-buffer  by    reader::eval-current-buffer
  1195. ;; eval-buffer        by    reader::eval-buffer
  1196. ;; original-eval-region by    reader::original-eval-region
  1197.  
  1198.  
  1199. ;; Temporary read buffer used for reading from strings
  1200. (defconst reader::tmp-buffer
  1201.   (get-buffer-create " *CL Read*"))
  1202.  
  1203. ;; Save a pointer to the original read function
  1204. (or (fboundp 'reader::original-read)
  1205.     (fset 'reader::original-read  (symbol-function 'read)))
  1206.  
  1207. (defun reader::read (&optional stream reader::recursive-p)
  1208.   "Read one Lisp expression as text from STREAM, return as Lisp object.
  1209. If STREAM is nil, use the value of `standard-input' \(which see\).
  1210. STREAM or the value of `standard-input' may be:
  1211.  a buffer \(read from point and advance it\)
  1212.  a marker \(read from where it points and advance it\)
  1213.  a string \(takes text from string, starting at the beginning\)
  1214.  t \(read text line using minibuffer and use it\).
  1215.  
  1216. This is the cl-read replacement of the standard elisp function
  1217. `read'. The only incompatibility is that functions as stream arguments
  1218. are not supported."
  1219.   (if (not cl-read-active)
  1220.       (reader::original-read stream)
  1221.     (if (null stream)            ; read from standard-input
  1222.     (setq stream standard-input))
  1223.  
  1224.     (if (eq stream 't)            ; read from minibuffer
  1225.     (setq stream (read-from-minibuffer "Common Lisp Expression: ")))
  1226.  
  1227.     (cond 
  1228.  
  1229.      ((bufferp stream)            ; read from buffer
  1230.       (reader::read-from-buffer stream reader::recursive-p))
  1231.  
  1232.      ((markerp stream)            ; read from marker
  1233.       (save-excursion 
  1234.     (set-buffer (marker-buffer stream))
  1235.     (goto-char (marker-position stream))
  1236.     (reader::read-from-buffer (current-buffer) reader::recursive-p)))
  1237.  
  1238.      ((stringp stream)            ; read from string
  1239.       (save-excursion
  1240.     (set-buffer reader::tmp-buffer)
  1241.     (auto-save-mode -1)
  1242.     (erase-buffer)
  1243.     (insert stream)
  1244.     (goto-char (point-min))
  1245.     (reader::read-from-buffer reader::tmp-buffer reader::recursive-p)))
  1246.      (t 
  1247.       (reader::error "Not a valid stream: %s" stream)))))
  1248.  
  1249. ;; read-from-string
  1250. ;; save a pointer to the original `read-from-string' function
  1251. (or (fboundp 'reader::original-read-from-string)
  1252.     (fset 'reader::original-read-from-string
  1253.       (symbol-function 'read-from-string)))
  1254.  
  1255. (defun reader::read-from-string (string &optional start end)
  1256.   "Read one Lisp expression which is represented as text by STRING.
  1257. Returns a cons: (OBJECT-READ . FINAL-STRING-INDEX).
  1258. START and END optionally delimit a substring of STRING from which to read;
  1259. they default to 0 and (length STRING) respectively.
  1260.  
  1261. This is the cl-read replacement of the standard elisp function
  1262. `read-from-string'.  It uses the reader macros in *readtable* if
  1263. `cl-read-active' is non-nil in the current buffer."
  1264.  
  1265.   ;; Does it really make sense to have read-from-string depend on
  1266.   ;; what the current buffer happens to be?   Yes, so code that
  1267.   ;; has nothing to do with cl-read uses original reader.
  1268.   (if (not cl-read-active)
  1269.       (reader::original-read-from-string string start end)
  1270.     (or start (setq start 0))
  1271.     (or end (setq end (length string)))
  1272.     (save-excursion
  1273.       (set-buffer reader::tmp-buffer)
  1274.       (auto-save-mode -1)
  1275.       (erase-buffer)
  1276.       (insert (substring string 0 end))
  1277.       (goto-char (1+ start))
  1278.       (cons 
  1279.        (reader::read-from-buffer reader::tmp-buffer nil)
  1280.        (1- (point))))))
  1281.  
  1282. ;; (read-from-string "abc (car 'a) bc" 4)
  1283. ;; (reader::read-from-string "abc (car 'a) bc" 4)
  1284. ;; (read-from-string "abc (car 'a) bc" 2 11)
  1285. ;; (reader::read-from-string "abc (car 'a) bc" 2 11)
  1286. ;; (reader::read-from-string "`(car ,first ,@rest)")
  1287. ;; (read-from-string ";`(car ,first ,@rest)")
  1288. ;; (reader::read-from-string ";`(car ,first ,@rest)")
  1289.  
  1290. ;; We should replace eval-expression, too, so that it reads (and
  1291. ;; evals) in the current buffer.  Alternatively, this could be fixed
  1292. ;; in C.  In Lemacs 19.6 and later, this function is already written
  1293. ;; in lisp, and based on more primitive read functions we already
  1294. ;; replaced. The reading happens during the interactive parameter
  1295. ;; retrieval, which is written in lisp, too.  So this replacement of
  1296. ;; eval-expression is only required for (FSF) Emacs 18 (and 19?).
  1297.  
  1298. (or (fboundp 'reader::original-eval-expression)
  1299.     (fset 'reader::original-eval-expression 
  1300.           (symbol-function 'eval-expression)))
  1301.  
  1302. (defun reader::eval-expression (reader::expression)
  1303.   "Evaluate EXPRESSION and print value in minibuffer.
  1304. Value is also consed on to front of variable `values'."
  1305.   (interactive 
  1306.    (list
  1307.     (car (read-from-string
  1308.           (read-from-minibuffer 
  1309.            "Eval: " nil 
  1310.            ;;read-expression-map ;; not for emacs 18
  1311.            nil ;; use default map
  1312.            nil ;; don't do read with minibuffer current.
  1313.            ;; 'edebug-expression-history ;; not for emacs 18
  1314.            )))))
  1315.   (setq values (cons (eval reader::expression) values))
  1316.   (prin1 (car values) t))
  1317.  
  1318. (require 'eval-reg "eval-reg")
  1319. ; (require 'advice)
  1320.  
  1321.  
  1322. ;; installing/uninstalling the cl reader
  1323. ;; These two should always be used in pairs, or just install once and
  1324. ;; never uninstall. 
  1325. (defun cl-reader-install ()
  1326.   (interactive)
  1327.   (fset 'read             'reader::read)
  1328.   (fset 'read-from-string     'reader::read-from-string)
  1329.   (fset 'eval-expression     'reader::eval-expression)
  1330.   (elisp-eval-region-install))
  1331.  
  1332. (defun cl-reader-uninstall ()
  1333.   (interactive)
  1334.   (fset 'read                
  1335.     (symbol-function 'reader::original-read))
  1336.   (fset 'read-from-string    
  1337.     (symbol-function 'reader::original-read-from-string))
  1338.   (fset 'eval-expression
  1339.     (symbol-function 'reader::original-eval-expression))
  1340.   (elisp-eval-region-uninstall))
  1341.  
  1342. ;; Globally installing the cl-read replacement functions is safe, even
  1343. ;; for buffers without cl read syntax. The buffer local variable
  1344. ;; `cl-read-active' controls whether the replacement funtions of this
  1345. ;; package or the original ones are actually called.
  1346. (cl-reader-install)
  1347. (cl-reader-uninstall)
  1348.  
  1349. (add-hook 'emacs-lisp-mode-hook 'cl-reader-autoinstall-function)
  1350.  
  1351. '(defvar read-syntax)
  1352.  
  1353. '(defun cl-reader-autoinstall-function () 
  1354.   "Activates the Common Lisp style reader for emacs-lisp-mode buffers,
  1355. if the property line has a local variable setting like this: 
  1356. \;\; -*- Read-Syntax: Common-Lisp -*-"
  1357.   ;; this is a hack to avoid recursion in the case that the prop line 
  1358.   ;; containes "Mode: emacs-lisp" entry
  1359.   (or (boundp 'local-variable-hack-done)
  1360.       (let (local-variable-hack-done
  1361.         (case-fold-search t))
  1362.     ;; Usually `hack-local-variables-prop-line' is called only after
  1363.     ;; installation of the major mode. But we need to know about the
  1364.     ;; local variables before that, so we call the local variable hack
  1365.     ;; explicitly here:
  1366.     (hack-local-variables-prop-line 't)
  1367.     ;; But hack-local-variables-prop-line not defined in emacs 18.
  1368.     (cond 
  1369.      ((and (boundp 'read-syntax)
  1370.            read-syntax
  1371.            (string-match "^common-lisp$" (symbol-name read-syntax)))
  1372.       (require 'cl-read)
  1373.       (make-local-variable 'cl-read-active)
  1374.       (setq cl-read-active 't))))))
  1375.  
  1376. ;; Emacs 18 doesnt have hack-local-variables-prop-line.  So use this instead.
  1377. (defun cl-reader-autoinstall-function ()
  1378.   (save-excursion
  1379.     (goto-char (point-min))
  1380.     (let ((case-fold-search t))
  1381.       (cond ((re-search-forward 
  1382.           "read-syntax: *common-lisp" 
  1383.           (save-excursion 
  1384.         (end-of-line)
  1385.         (point))
  1386.           t)
  1387.          (require 'cl-read)
  1388.          (make-local-variable 'cl-read-active)
  1389.          (setq cl-read-active t))))))
  1390.  
  1391.  
  1392. (run-hooks 'cl-read-load-hooks)
  1393.  
  1394. ;; cl-read.el ends here
  1395.